package com.leo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * @ClassName MouseDemo
 * @Description 拖拽功能以及窗体样式设计
 * @Author wangss
 * @date 2020.06.04 22:37
 * @Version 1.0
 */
public class MouseDemo6 extends Application {

    private double buttonHeight;
    private double buttonWidth;
    private double c;
    private double d;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        Scene scene = new Scene(an);
        scene.setFill(Paint.valueOf("#FFFFFF00"));

        Background background = new Background(new BackgroundFill(Paint.valueOf("#FFB5C5"), new CornerRadii(30), new Insets(0)));
        an.setBackground(background);

        an.setBorder(new Border(new BorderStroke(Color.valueOf("#912CEE"), BorderStrokeStyle.SOLID, new CornerRadii(30), new BorderWidths(2))));

        Button b1 = new Button("button1");

        AnchorPane.setTopAnchor(b1, 100d);
        AnchorPane.setLeftAnchor(b1, 100d);
        an.getChildren().add(b1);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ImageViewDemo");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.show();

        b1.setOnMousePressed(event -> {
            buttonHeight = event.getY();
            buttonWidth = event.getX();
        });

        b1.setOnMouseDragged(event -> {
            AnchorPane.setTopAnchor(b1, event.getSceneY() - buttonHeight);
            AnchorPane.setLeftAnchor(b1, event.getSceneX() - buttonWidth);
        });

        scene.setOnMousePressed(event -> {
            c = event.getScreenX() - primaryStage.getX();
            d = event.getScreenY() - primaryStage.getY();
        });

        scene.setOnMouseDragged(event -> {
            primaryStage.setX(event.getScreenX() - c);
            primaryStage.setY(event.getScreenY() - d);
        });

        b1.setOnMouseClicked(event -> Platform.exit());
    }
}
